package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import View.MainFrame;

/**
 * Implements action for the main frame
 * @author Vincent Francolin
 * @version 1.0 : creation
 */
public class MainFrameActionListener implements ActionListener {
	MainFrame obj;
	ContactController ctrl;
	
	/**
	 * default constructor
	 * @param obj the main frame
	 * @param ctrl the main controller of the app
	 */
	public MainFrameActionListener(MainFrame obj,ContactController ctrl) {
		super();
		this.obj = obj;
		this.ctrl = ctrl;
	}
	
	/**
	 * method called whenever an action happens
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JButton src = (JButton)e.getSource();
		if( src.getText().equals("Add a contact") ){
			System.out.println("add button");
			obj.changeFormView(null,null,ctrl);
		} else if( src.getText().equals("Modify a contact") ){
			System.out.println("modify a contact");
			if(ctrl.getCurrentSelected()!=-1)
				obj.changeFormView(ctrl.getContact(ctrl.getCurrentSelected()),
						ctrl.getContactGroup(ctrl.getCurrentSelected()),ctrl);
			//find a way to propagate the modification in contactList on save
		} else if( src.getText().equals("Delete a contact") ){
			System.out.println("delete a contact");
			System.out.println("id : "+ctrl.getCurrentSelected());
			//obj.removeContactFromView(ctrl.getCurrentSelected());
			if(ctrl.getCurrentSelected()!=-1)
				ctrl.removeContact(ctrl.getCurrentSelected());
		} else if( src.getText().equals("Create a group") ){
			System.out.println("create a group");
			String s = (String)JOptionPane.showInputDialog(
                    null,
                    "Enter the name of the group",
                    "Customized Dialog",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    null,
                    "Enter the group name");
			if ((s != null) && (s.length() > 0)) {
			    ctrl.addGroup(s);
			}
		}
		
	}

}
